#include <iostream>
#include <queue>
#include <string>
using namespace std;

const int kMaxN = 510;
const int kMaxM = 510;
const int kInf = 1e9;

queue<pair<int, int>> que[kMaxN + kMaxM + 2];
string g[kMaxN];
int dist[kMaxN][kMaxM];

bool Check(int x, int y, int n, int m) {
  return x >= 0 && x < n && y >= 0 && y < m;
}

int BFS(int sx, int sy, int ex, int ey, int n, int m) {
  int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

  dist[sx][sy] = 0;
  que[0].emplace(sx, sy);
  for (int d = 0; d < n + m + 2; d++) {
    while (que[d].size()) {
      int x = que[d].front().first, y = que[d].front().second;

      que[d].pop();
      if (dist[x][y] != d) {
        continue;
      }
      for (int i = 0; i < 4; i++) {
        int nx = x + dir[i][0], ny = y + dir[i][1];

        if (Check(nx, ny, n, m) && d + (g[x][y] != g[nx][ny]) < dist[nx][ny]) {
          dist[nx][ny] = d + (g[x][y] != g[nx][ny]);
          que[dist[nx][ny]].emplace(nx, ny);
        }
      }
    }
  }
  return dist[ex][ey];
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;

  while (cin >> n >> m) {
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      fill(dist[i], dist[i] + m, kInf);
    }
    for (int i = 0; i < n; i++) {
      cin >> g[i];
    }
    int sx, sy, ex, ey;

    cin >> sx >> sy >> ex >> ey;
    cout << BFS(sx, sy, ex, ey, n, m) << "\n";
  }
  return 0;
}